﻿//Paging configuration - IMPORTANT

function CreatePagingNumber(number, size, totals) {
    var page = Math.round(totals / size);
    if (eval(totals) % eval(size) > 0) page++;
    if (page > 0) {
        $('#dataTables_paginate span').html('');
        for (var i = 1; i <= page; i++) {
            $('#dataTables_paginate span').append('<a class="paginate_button" tabindex="0">' + (i) + '</a>');
            if (i == 5) {
                $('#dataTables_paginate span').append('<a class="paginate_button" tabindex="0">...</a>');
                break;
            }
        }
        $('#dataTables_paginate span a:eq(' + number + ')').removeClass('paginate_button');
        $('#dataTables_paginate span a:eq(' + number + ')').addClass('paginate_active');
    }
}

//First Previous Next Last
function FPNL(idTable, pageSize, totals) {
    $('#' + idTable + '_first').click(function () {
        getDataFromDatabase(idTable, 1, pageSize, totals);
        CreatePagingNumber(0, pageSize, totals);
        showMorePage(idTable, pageSize, totals);
    });

    $('#' + idTable + '_last').click(function () {
        var m = Math.round(totals / pageSize);
        if (totals % pageSize > 0) m++;
        getDataFromDatabase(idTable, m, pageSize, totals);
        if (m - 3 > 0) {
            $('#dataTables_paginate span').html('<a class="paginate_button" tabindex="0">...</a>');
            $('#dataTables_paginate span').append('<a class="paginate_button" tabindex="0">' + (eval(m) - 2) + '</a>');
            $('#dataTables_paginate span').append('<a class="paginate_button" tabindex="0">' + (eval(m) - 1) + '</a>');
            $('#dataTables_paginate span').append('<a class="paginate_button" tabindex="0">' + m + '</a>');
        }
        setCurrentPage(m, idTable);
        $('#dataTables_paginate span a').click(function () {
            $('#dataTables_paginate span a').each(function () {
                $(this).removeClass('paginate_active');
                $(this).addClass('paginate_button');
            });
            getDataFromDatabase(idTable, $(this).text(), pageSize, totals);
            $(this).removeClass('paginate_button');
            $(this).addClass('paginate_active');
            showMorePage(idTable, pageSize, totals);
        });
    });

    $('#' + idTable + '_previous').click(function () {
        var current = $('.paginate_active').text();
        if (current > 1) {
            getDataFromDatabase(idTable, eval(current) - 1, pageSize, totals);
            setCurrentPage(eval(current) - 1, idTable);
        }
    });

    $('#' + idTable + '_next').click(function () {
        var current = $('.paginate_active').text();
        if (current < Math.round(totals / pageSize)) {
            getDataFromDatabase(idTable, eval(current) + 1, pageSize, totals);
            setCurrentPage(eval(current) + 1, idTable);
        }
    });
}

//Set current page
function setCurrentPage(current, idTable) {
    $('#' + idTable + '_paginate span a').each(function () {
        if ($(this).text() != current) {
            $(this).removeClass('paginate_active');
            $(this).addClass('paginate_button');
        } else {
            $(this).removeClass('paginate_button');
            $(this).addClass('paginate_active');
        }
    });
}

//Show total-records
function showTotalRecords(idTable, current, pagesize, totals) {
    var s;
    if ((eval(current) + 1) * pagesize > totals) {
        s = totals;
    } else {
        s = (eval(current) + 1) * pagesize;
    }
    $('#' + idTable + '_info').html('Display from ' + eval(eval(current * pagesize) + 1) + ' to ' + s + ' of ' + totals + ' record(s)');
}

function configurationPaging(idTable, pageNumber, size, totals) {
    CreatePagingNumber(pageNumber, size, totals);
    $('#dataTables_paginate span a').click(function () {
        $('#dataTables_paginate span a').each(function () {
            $(this).removeClass('paginate_active');
            $(this).addClass('paginate_button');
        });
        getDataFromDatabase(idTable, $(this).text(), size, totals);
        $(this).removeClass('paginate_button');
        $(this).addClass('paginate_active');
        //todo
        showMorePage(idTable, size, totals);
    });
    FPNL(idTable, size, totals);
    showTotalRecords(idTable, pageNumber, size, totals);
}

function showMorePage(idTable, size, totals) {
    var current = $('.paginate_active').first().text();
    if (eval(current) > 3) {
        $('#dataTables_paginate span').html('<a class="paginate_button" tabindex="0">...</a>');
        $('#dataTables_paginate span').append('<a class="paginate_button" tabindex="0">' + (eval(current) - 2) + '</a>');
        $('#dataTables_paginate span').append('<a class="paginate_button" tabindex="0">' + (eval(current) - 1) + '</a>');
        $('#dataTables_paginate span').append('<a class="paginate_button" tabindex="0">' + (eval(current)) + '</a>');
        var max = Math.round(totals / size);
        if (totals % size > 0) max++;
        if (eval(current) < max - 3) {
            $('#dataTables_paginate span').append('<a class="paginate_button" tabindex="0">' + (eval(current) + 1) + '</a>');
        }
        if (eval(current) < max - 1) {
            $('#dataTables_paginate span').append('<a class="paginate_button" tabindex="0">' + (eval(current) + 2) + '</a>');
            $('#dataTables_paginate span').append('<a class="paginate_button" tabindex="0">' + (eval(current) + 3) + '</a>');
            $('#dataTables_paginate span').append('<a class="paginate_button" tabindex="0">...</a>');
        }
        setCurrentPage(current, idTable);
    } else {
        CreatePagingNumber(0, size, totals);
        setCurrentPage(current, idTable);
    }
    $('#dataTables_paginate span a').click(function () {
        $('#dataTables_paginate span a').each(function () {
            $(this).removeClass('paginate_active');
            $(this).addClass('paginate_button');
        });
        getDataFromDatabase(idTable, $(this).text(), size, totals);
        $(this).removeClass('paginate_button');
        $(this).addClass('paginate_active');
        //todo
        showMorePage(idTable, size, totals);
    });
}